﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
namespace ZF.WS.Admin
{

    public partial class PopularizeCompanyManage : PopularizeCompany 
    {
    

        [PageArgument(Key = "id", CanNull = false, BindOperType = PageOperType.Edit)]
        public int  Id { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {

            this.ddlType.DataSource = BLL.DictionaryConfig.APopularizeCompanyType;
            this.ddlType.DataBind();
     
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var popularize = new ZF.Model.AD_Popularize()
                {
                    Address = this.tbAddress.Text,
                    AddTime = DateTime.Now,
                    CompanyName = this.tbName.Text,
                    Contact = this.tbContact.Text,
                    Email = this.tbEmail.Text,
                    Info = this.tbInfo.Text,
                    OrderId = this.tbOrderId.Text.EmptyInt(0),
                    Tel = this.tbTel.Text,
                    TypeId =   this.ddlType.SelectedValue.Convert<int>(),
                    Zip = this.tbZipcode.Text,
                    Url = this.tbUrl.Text.EmptyString(),
                    Logo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
                };


                try
                {
                    dc.AD_Popularize.InsertOnSubmit(popularize);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }

                string msgKey = CTitle;

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey), Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

 
            }
             

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var popularize = dc.AD_Popularize.FirstOrDefault(p => p.PopularizeId == Id);
                if (popularize == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                InitControls();
                this.tbAddress.Text = popularize.Address;
                this.tbName.Text = popularize.CompanyName;
                this.tbContact.Text = popularize.Contact;
                this.tbEmail.Text = popularize.Email;
                this.tbInfo.Text = popularize.Info;
                this.tbOrderId.Text = popularize.OrderId.ToString();
                this.tbTel.Text = popularize.Tel;
                this.tbZipcode.Text = popularize.Zip;
                this.tbUrl.Text = popularize.Url;
                this.tbPhoto.Text = popularize.Logo;
                this.ddlType.SelectedValue = popularize.TypeId.ToString();
                this.lvImgList.DataSource = popularize.Logo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();


            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {
                    var popularize = dc.AD_Popularize.FirstOrDefault(p => p.PopularizeId == this.Id);
                    if (popularize == null)
                        throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                    popularize.Address = this.tbAddress.Text;
                    popularize.CompanyName = this.tbName.Text;
                    popularize.Contact = this.tbContact.Text;
                    popularize.Email = this.tbEmail.Text;
                    popularize.Info = this.tbInfo.Text;
                    popularize.OrderId = this.tbOrderId.Text.EmptyInt(0);
                    popularize.Tel = this.tbTel.Text;
                    popularize.Zip = this.tbZipcode.Text;
                    popularize.Url = this.tbUrl.Text.EmptyString();
                    popularize.Logo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";");
                    popularize.TypeId = this.ddlType.SelectedValue.Convert<int>();
                    var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                    popularize.Logo  = photoList.ConstituteString(";");

                     
                    try
                    {
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = CTitle;

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功修改一个{0}，点击确定按钮返回{0}列表", msgKey));

                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey), Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success, ListPage).Alert();

                }


            }

        }

    
    }
 
}
